package com.easy.framework.common.model;

import cn.hutool.core.map.MapUtil;
import com.easy.framework.common.utils.Func;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Query基类
 *
 * @author Easy
 */
@Setter
@Getter
public class BaseQuery implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String DATA_SCOPE = "dataScope";


    /**
     * 页码
     */
    @Min(1)
    private Integer pageNum = 1;

    /**
     * 页数
     */
    @Min(1)
    @Max(1000)
    private Integer pageSize = 20;

    /**
     * 合理分页
     */
    private Boolean reasonable = true;

    /**
     * 请求参数
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Map<String, Object> params;

    private OrderBy orderBy;

    public Map<String, Object> getParams() {
        if (params == null) {
            params = new HashMap<>();
        }
        return params;
    }

    public String getDataScope() {
        if (Func.isNotEmpty(params)) {
            return MapUtil.getStr(params, DATA_SCOPE, null);
        }
        return null;
    }

    public void setDataScope(String dataScope) {
        getParams().put(DATA_SCOPE, dataScope);
    }

    public void clearDateScope() {
        if (Func.isNotEmpty(params)) {
            params.remove(DATA_SCOPE);
        }
    }


    public Integer getOffset() {
        return (pageNum - 1) * pageSize;
    }
}
